<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="客户编码:">
        <el-input v-model="queryParams.customerCode" size="small" placeholder="请输入客户编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称:">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:customer:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['basic:customer:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col> -->
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['basic:customer:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:customer:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleLogicalDelete">作废</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" :cell-style="cellStyle" highlight-current-row @row-click="handleRowClick" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
    <!--  <el-table-column label="客户编码" align="center" prop="customerNo" /> -->

      <!-- <el-table-column label="商城类型" align="center" prop="customerType">
        <template slot-scope="scope">
          <span>{{ customerTypeFormat(scope.row) }}</span>
        </template>
      </el-table-column> -->
     <el-table-column label="客户名称" align="center" show-overflow-tooltip width="188" prop="customerName">
          <template slot-scope="scope">
            {{ scope.row.customerName }}
<!--            <el-button type="text" @click="handleView(scope.row)">{{ scope.row.customerName }}</el-button>-->
          </template>
        </el-table-column>
      <el-table-column label="营业执照号" width="125" align="center" show-overflow-tooltip prop="bizLicenceNo" />
      <el-table-column label="开户银行" width="165" align="center" prop="bankName" />
      <el-table-column label="银行账户" width="165" align="center" show-overflow-tooltip prop="bankAccount" />
      <el-table-column label="联系人姓名" align="center" prop="contactName" />
      <el-table-column label="联系人电话" width="125" align="center" show-overflow-tooltip prop="contactPhone" />
      <el-table-column label="联系人邮件" align="center" prop="contactEmail" />
      <el-table-column label="企业名称" width="165" align="center" prop="companyName" />
      <el-table-column label="联系人" width="165" align="center" prop="contactWechatId" />
      <el-table-column label="网址" width="165" align="center" prop="webSite" />
      <el-table-column label="微信号" width="125" show-overflow-tooltip align="center" prop="wechatId" />
      <el-table-column label="入驻时间" align="center" prop="enrollDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enrollDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开业时间" align="center" prop="openDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:customer:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
       <!--   <el-button v-hasPermission="['basic:customer:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
       --> </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />
    <customer-detail v-if="rowId && detailVisible" :id="rowId" class="detail-view" @close="handleDetailClose" @callback="handleQuery" />
    <customer-save v-if="editVisible" :id="detail.id" @callback="handleQuery" @close="editVisible = false" />
  </div>
</template>

<script>
import ListMixin from '@/mixins/List'
import CustomerApi from '@/api/basic/CustomerApi'
import CustomerDetail from './CustomerDetail.vue'
import CustomerSave from '@/views/basic/customer/CustomerSave'

export default {
  name: 'CustomerList',
  components: {
    CustomerDetail,
    CustomerSave
  },
  mixins: [ListMixin],
  data() {
    return {
      options: {
        customerTypeOptions: [], // 客户类型字典
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        customerCode: null,
        customerName: null,
        customerType: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.detail = {}
      this.editVisible = true
    },
    handleView(row) {
      this.$router.push({ path: '/customer/view/' + row.id })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerApi.logicalDelete(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      this.detail = row
      this.editVisible = true
    },
    handleQuery() {
      this.loading = true
      CustomerApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      const that = this
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerApi.exportData(that.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    // 客户类型字典
    customerTypeFormat(row) {
      return this.$common.getDictValueName(this.options.customerTypeOptions, row.customerType)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    handleRowClick(row, column, event) {
      if (column.type === 'selection') {
        return
      }
      if (column.property === 'customerName') {
        this.rowId = row.id
        this.detailVisible = true
      } else {
        this.detailVisible = false
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'customerName') {
        return {
          color: '#3E84E9',
          cursor: 'pointer'
        }
      } else {
        return ''
      }
    },
    resetQuery() {
      this.page.current = 1
      this.queryParams = {
        customerCode: null,
        customerName: null,
        customerType: null,
        isValid: this.$constant.Y
      }
      this.handleQuery()
    }
  }
}
</script>
